package com.powerbank.admin.controller;

import com.powerbank.common.result.Result;
import com.powerbank.admin.vo.DashboardVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 管理后台首页控制器
 */
@Slf4j
@RestController
@RequestMapping("/dashboard")
public class DashboardController {

    /**
     * 获取仪表盘数据
     */
    @GetMapping("/overview")
    public Result<DashboardVO> getDashboardOverview(HttpServletRequest request) {
        Long adminId = (Long) request.getAttribute("adminId");
        log.info("管理员{}查看仪表盘数据", adminId);

        try {
            DashboardVO dashboard = new DashboardVO();
            
            // 模拟数据
            dashboard.setTotalUsers(12580L);
            dashboard.setActiveUsers(8960L);
            dashboard.setTotalDevices(456L);
            dashboard.setOnlineDevices(423L);
            dashboard.setTodayOrders(1234L);
            dashboard.setTodayRevenue(new BigDecimal("25678.50"));
            dashboard.setMonthRevenue(new BigDecimal("768945.20"));
            
            // 趋势数据
            Map<String, Object> trends = new HashMap<>();
            trends.put("userGrowth", "+12.5%");
            trends.put("orderGrowth", "+8.3%");
            trends.put("revenueGrowth", "+15.2%");
            trends.put("deviceUtilization", "87.5%");
            dashboard.setTrends(trends);
            
            dashboard.setLastUpdateTime(LocalDateTime.now());
            
            return Result.success("仪表盘数据获取成功", dashboard);
        } catch (Exception e) {
            log.error("获取仪表盘数据失败：{}", e.getMessage(), e);
            return Result.error("获取仪表盘数据失败：" + e.getMessage());
        }
    }

    /**
     * 获取系统状态
     */
    @GetMapping("/system-status")
    public Result<Map<String, Object>> getSystemStatus(HttpServletRequest request) {
        Long adminId = (Long) request.getAttribute("adminId");
        log.info("管理员{}查看系统状态", adminId);

        try {
            Map<String, Object> status = new HashMap<>();
            status.put("systemVersion", "1.0.0");
            status.put("uptime", "7天8小时30分钟");
            status.put("cpuUsage", "65%");
            status.put("memoryUsage", "78%");
            status.put("diskUsage", "45%");
            status.put("networkStatus", "正常");
            status.put("databaseStatus", "正常");
            status.put("redisStatus", "正常");
            
            return Result.success("系统状态获取成功", status);
        } catch (Exception e) {
            log.error("获取系统状态失败：{}", e.getMessage(), e);
            return Result.error("获取系统状态失败：" + e.getMessage());
        }
    }
}